package checkers3d.logic;

/**
 *
 * @author rherbzi
 * @author Ruben Acuna
 */

import checkers3d.presentation.GUIButton;
import checkers3d.presentation.GUIContainer;
import checkers3d.presentation.GUILabel;
import checkers3d.presentation.GUIPictureBox;
import checkers3d.presentation.GUITextbox;
import checkers3d.presentation.GameEngine;
import checkers3d.presentation.IDrawable;
import checkers3d.presentation.IInputObserver;
import checkers3d.presentation.UtilGUI;
import checkers3d.storage.DataManagerAccounts;
import checkers3d.storage.DataManagerStats;
import java.awt.Color;
import java.awt.Point;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MenuAdminChangeStats  extends GUIContainer{
    private final int CONTROL_BUTTON_AXIS = GameEngine.WINDOW_SIZE.y - 100;
    private final String promptInstructions = "Enter a name and click the desired option.";

    GUITextbox playerName;
    GUILabel   playerValidation;
    GUITextbox playerWins;
    GUITextbox playerLosses;
    GUITextbox playerTies;

    GUIButton buttonPlayerChangeStats;
    GUIButton buttonPlayerClearStats;
    GUIButton buttonPlayerDelete;
    GUIButton buttonPlayerRemoveStats;
    GUIButton buttonReturn;

    String lastValidatedName = "";

    public MenuAdminChangeStats(Point size){
        super(size);

        //HACK:
        int xOffset = 80;
        int yOffset = 200;

        LinkedList<IDrawable> buttonRow1 = new LinkedList<IDrawable>();
        LinkedList<IDrawable> buttonRow2 = new LinkedList<IDrawable>();

        //add background image.
        add(new GUIPictureBox("Background.png", new Point()));

        buttonPlayerRemoveStats = new GUIButton(".\\images\\button-stats-player-removestats.png", null, new ObserverNull());
        buttonRow1.add(buttonPlayerRemoveStats);

        buttonPlayerChangeStats = new GUIButton(".\\images\\button-stats-player-changestats.png", null, new ObserverNull());
        buttonRow2.add(buttonPlayerChangeStats);

        buttonPlayerClearStats = new GUIButton(".\\images\\button-stats-player-clearstats.png", null, new ObserverNull());
        buttonRow1.add(buttonPlayerClearStats);

        buttonPlayerDelete = new GUIButton(".\\images\\button-stats-player-remove.png", null, new ObserverNull());
        buttonRow1.add(buttonPlayerDelete);

        //create the return button with behavior.
        buttonReturn = new GUIButton("button-return.png", null, new ObserverReturn());
        buttonRow2.add(buttonReturn);

        add(new GUILabel(promptInstructions, Color.WHITE, new Point(124 + xOffset, 30 + yOffset)));
        add(new GUILabel("Name:", Color.WHITE, new Point(102 + xOffset, 70 + yOffset)));
        add(new GUILabel("wins:", Color.WHITE, new Point(102 + xOffset, 90 + yOffset)));
        add(new GUILabel("losses:", Color.WHITE, new Point(102 + xOffset, 110 + yOffset)));
        add(new GUILabel("ties:", Color.WHITE, new Point(102 + xOffset, 130 + yOffset)));
        playerValidation = new GUILabel("available", Color.RED, new Point(207 + xOffset, 50 + yOffset));
        add(playerValidation);

        //add the textbox for player name and password.

        playerName = new GUITextbox(new Point(166 + xOffset, 70 + yOffset), 25, this);
        playerWins = new GUITextbox(new Point(166 + xOffset, 90 + yOffset), 25, this);
        playerLosses = new GUITextbox(new Point(166 + xOffset, 110 + yOffset), 25, this);
        playerTies = new GUITextbox(new Point(166 + xOffset, 130 + yOffset), 25, this);

         for(IDrawable component : buttonRow1)
            add(component);

         for(IDrawable component : buttonRow2)
            add(component);

        //compute the positions of the buttons
        UtilGUI.computeLayoutSpanning(getSize(), CONTROL_BUTTON_AXIS - 100, buttonRow1);
        UtilGUI.computeLayoutSpanning(getSize(), CONTROL_BUTTON_AXIS, buttonRow2);
    }

    private void validatePlayers()
    {
        try
        {
            String player = playerName.getText();
            if (DataManagerAccounts.doesPlayerExist(player))
            {
                playerValidation.setColor(Color.GREEN);
            }
            else
            {
                lastValidatedName = "";
                playerValidation.setColor(Color.RED);
            }
        }
        catch(Exception error)
        {
            Logger.getLogger(MenuViewStats.class.getName()).log(Level.SEVERE, null, error);
        }
    }

    @Override
    public void onKeyPress(char key)
    {
        super.onKeyPress(key);

        validatePlayers();

        if((playerValidation.getColor() == Color.GREEN) )
        {
            lastValidatedName = playerName.getText();
            Player player = DataManagerAccounts.loadPlayer(playerName.getText());

                try {
                    playerWins.setText(DataManagerStats.getWins(player));
                    playerLosses.setText(DataManagerStats.getLosses(player));
                    playerTies.setText(DataManagerStats.getTies(player));
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
                }


            buttonPlayerRemoveStats.setObserver(new ObserverPlayerRemoveStats());
            buttonPlayerChangeStats.setObserver(new ObserverPlayerChangeStats());
            buttonPlayerClearStats.setObserver(new ObserverPlayerClearStats());
            buttonPlayerDelete.setObserver(new ObserverPlayerDelete());
        }
        else if((playerValidation.getColor()) == Color.RED)
        {
            buttonPlayerRemoveStats.setObserver(new ObserverNull());
            buttonPlayerChangeStats.setObserver(new ObserverNull());
            buttonPlayerClearStats.setObserver(new ObserverNull());
            buttonPlayerDelete.setObserver(new ObserverNull());

            playerWins.setText("0");
            playerLosses.setText("0");
            playerTies.setText("0");
        }


        if((playerWins.getText()).equals(""))
            playerWins.setText("0");

        if((playerLosses.getText()).equals(""))
            playerLosses.setText("0");

        if((playerTies.getText()).equals(""))
            playerTies.setText("0");
    }

   private class ObserverReturn implements IInputObserver
    {
        /**
         * Returns the playerName to the scenario select screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            GameMaster.getInstance().removeTopGUIContainer();
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverNull implements IInputObserver
    {

        public void onClick(Point position)
        {

        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverPlayerClearStats implements IInputObserver
    {
        public void onClick(Point position)
        {
            try {
                DataManagerStats.deleteStatistics();
            } catch (IOException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverPlayerDelete implements IInputObserver
    {
        public void onClick(Point position)
        {
            try {
                Player player = DataManagerAccounts.loadPlayer(playerName.getText());
                 DataManagerStats.removePlayerStats(player);
                DataManagerAccounts.removePlayer(playerName.getText());

            } catch (FileNotFoundException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverPlayerRemoveStats implements IInputObserver
    {
        public void onClick(Point position)
        {
            try {
                Player player = DataManagerAccounts.loadPlayer(playerName.getText());
                DataManagerStats.removePlayerStats(player);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverPlayerChangeStats implements IInputObserver
    {
        /**
         * Returns the playerName to the scenario select screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            try {
                Player player = DataManagerAccounts.loadPlayer(playerName.getText());
                int wins1 = Integer.parseInt(playerWins.getText());
                int losses1 = Integer.parseInt(playerLosses.getText());
                int ties1 = Integer.parseInt(playerTies.getText());
                DataManagerStats.setWins(wins1, player);
                DataManagerStats.setLosses(player, losses1);
                DataManagerStats.setTies(player, ties1);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MenuAdminChangeStats.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }
}